﻿#region

using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService
{
    public partial class AdminLogin : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            var user = new User
            {
                Username = txtUsername.Text,
                Password = txtPassword.Text
            };

            if (UserBLL.LoginUser(user))
            {
                if (!UserBLL.IsBannedUser(user))
                {
                    var admin = AdministratorBLL.GetAdministrator(txtUsername.Text);

                    if (admin != null)
                    {
                        if (!Roles.RoleExists(admin.Role))
                        {
                            Roles.CreateRole(admin.Role);
                        }
                        if (!Roles.IsUserInRole(admin.StaffId.ToString(), admin.Role))
                        {
                            Roles.AddUserToRole(admin.StaffId.ToString(), admin.Role);
                        }
                        FormsAuthentication.SetAuthCookie(admin.StaffId.ToString(), false);
                        Response.Redirect("AdminPanel/Default.aspx");
                    }
                    else
                    {
                        lblStatus.Text = "Invalid combination of Username & Password";
                        lblStatus.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblStatus.Text = "Sorry your account is banned";
                    lblStatus.ForeColor = Color.Red;
                }
            }
            else
            {
                lblStatus.Text = "Invalid combination of Username & Password";
                lblStatus.ForeColor = Color.Red;
            }
        }
    }
}